<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script>

		$(function () {
			listSpecification();
		})
		/*ajax请求获取规格信息*/
		function listSpecification(){
			var specName = $("#selectListBySpecName").val();
			$.post("/specification/listSpecification",{specName:specName},function (result) {
				//字符串拼接tr td
				let str='';
				console.log(result)
				for (let i = 0; i < result.data.length; i++){
					str+='<tr >'
					str+='  <td><input name="ids" type="checkbox" value="'+result.data[i].id+'"></td>'
					str+='  <td>'+result.data[i].id+'</td>'
					str+=' <td>'+result.data[i].specName+'</td>'
					str+='  <td class="text-center">'
					str+='<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateSpecificationModal" onclick="updateSpecificationTableEcho('+result.data[i].id+')">修改</button>'
					str+='</td>'
					str+='</tr>'
				}
				//覆盖tbody内容
				$("#listSpecification").html(str)
			},"json")
		}

	</script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteBrand()"><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input  type="text" id="selectListBySpecName">
									<button class="btn btn-default" onclick="listSpecification()" >查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="listSpecification">
			                         <!-- <tr >
			                              <td><input  type="checkbox" ></td>			                              
				                          <td>1</td>
									      <td>网络制式</td>
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           
		                                  </td>
			                          </tr>
									  <tr >
			                              <td><input  type="checkbox" ></td>			                              
				                          <td>2</td>
									      <td>服装尺码</td>
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input  class="form-control" placeholder="规格名称" name="specName" id="specName" >  </td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" title="新建" onclick="insertNode()"><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table class="table table-bordered table-striped table-hover dataTable" id="specificationTable">
                    <thead>
						<tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
						</tr>
                    </thead>
                    <tbody id="specification">
                      <tr>
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" onclick="deleteNode(this)" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <!--<tr>
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr>
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>-->
                    </tbody>
			  </table> 
			
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insertSpecification()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>



  <div class="modal fade" id="updateSpecificationModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">规格编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px" id="updateSpecificationEcho">
					 <!-- <tr>
						  <td>规格名称</td>
						  <td><input  class="form-control" placeholder="规格名称" name="specName" id="specName" >  </td>
					  </tr>-->
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" onclick="insertNodeUpdateTable()"><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table class="table table-bordered table-striped table-hover dataTable" id="updateSpecificationTable">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </tr>
					  </thead>
					  <tbody id="updateSpecificationOptionEcho">
					 <!-- <tr>
						  <td>
							  <input  class="form-control" placeholder="规格选项">
						  </td>
						  <td>
							  <input  class="form-control" placeholder="排序">
						  </td>
						  <td>
							  <button type="button" class="btn btn-default" title="删除" onclick="deleteNode(this)" ><i class="fa fa-trash-o"></i> 删除</button>
						  </td>
					  </tr>
					  <tr>
				            <td>
				            	<input  class="form-control" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
					  <tr>
				            <td>
				            	<input  class="form-control" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>-->
					  </tbody>
				  </table>


			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateSpecification()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
<script>
	//添加规格方法页面 添加规格选项
	function insertNode() {
		var putTr = document.createElement("tr")
		let str='<td>'
		str+='<input  class="form-control" placeholder="规格选项"> '
		str+='</td>'
		str+='<td>'
		str+='<input  class="form-control" placeholder="排序"> '
		str+=' </td>'
		str+='<td>'
		str+='<button type="button" class="btn btn-default" title="删除" onclick="deleteNode(this)"><i class="fa fa-trash-o"></i> 删除</button>'
		str+='</td>'
		putTr.innerHTML= str;
		document.getElementById("specification").appendChild(putTr)
	}

	//修改规格方法页面 添加规格选项
	function insertNodeUpdateTable() {
		var putTr = document.createElement("tr")
		let str='<td>'
		str+='<input  class="form-control" placeholder="规格选项"> '
		str+='</td>'
		str+='<td>'
		str+='<input  class="form-control" placeholder="排序"> '
		str+=' </td>'
		str+='<td>'
		str+='<button type="button" class="btn btn-default" title="删除" onclick="deleteNodeUpdateTable(this)"><i class="fa fa-trash-o"></i> 删除</button>'
		str+='</td>'
		putTr.innerHTML= str;
		document.getElementById("updateSpecificationOptionEcho").appendChild(putTr)
	}
	//添加规格方法页面 删除规格选项
	function deleteNode(obj) {
		if(confirm("确定要删除么")){
			document.getElementById("specification").removeChild(obj.parentNode.parentNode);
		}
	}
	//修改规格方法页面 修改规格选项
	function deleteNodeUpdateTable(obj) {
		if(confirm("确定要删除么")){
			document.getElementById("updateSpecificationOptionEcho").removeChild(obj.parentNode.parentNode);
		}
	}

	//增加规格方法
	function insertSpecification() {
		const optionList = [];
		const trs = $("#specificationTable tbody tr")
		for(let i=0; i<trs.length; i++){
			const tds = $(trs[i]).find("td");
			const optionvalue = $(tds[0]).find("input").val();
			const order = $(tds[1]).find("input").val();
			const options = {"optionName": optionvalue, "orders": order};
			optionList.push(options);//对象js 对象 java不好接受，把对象转成json字符串，后台java转成list集合
		}
		var specName = $("#specName").val();
		var json = JSON.stringify(optionList);
		//ajax请求增加商品
		$.post("/specification/insertSpecification",{specName:specName,json:json},function (result) {
			if("ok"==result.message){
				window.location.reload()
			}else {
				alert("添加失败")
			}

		})
	}
	//ajax请求回显修改页面
	function updateSpecificationTableEcho(id) {

		$.post("/specification/getSpecification?id="+id,function (result) {
			//拼接tr td
			let specificationEcho='<tr>'
			specificationEcho+='  <td>规格名称</td>'
			specificationEcho+='<td><input  class="form-control" placeholder="规格名称" name="specName" id="updateSpecName" value="'+result.info.specName+'"> </td>'
			specificationEcho+='<input type="hidden" class="form-control" name="specName" id="updateId" value="'+result.info.id+'"> </td>'
			specificationEcho+='</tr>'
			$("#updateSpecificationEcho").html(specificationEcho)


			let specificationOptionEcho=''
			for (let i = 0; i<result.info.otions.length; i++){
				specificationOptionEcho+='<tr>'
				specificationOptionEcho+='<td>'
				specificationOptionEcho+='<input  class="form-control" placeholder="规格选项" value="'+result.info.otions[i].optionName+'">'
				specificationOptionEcho+=' </td>'
				specificationOptionEcho+=' <td>'
				specificationOptionEcho+='<input  class="form-control" placeholder="排序" value="'+result.info.otions[i].orders+'">'
				specificationOptionEcho+='</td>'
				specificationOptionEcho+='<td>'
				specificationOptionEcho+='<button type="button" class="btn btn-default" title="删除" onclick="deleteNodeUpdateTable(this)"><i class="fa fa-trash-o"></i> 删除</button>'
				specificationOptionEcho+='</td>'
				specificationOptionEcho+='</tr>'
			}
			$("#updateSpecificationOptionEcho").html(specificationOptionEcho)
		})
	}

	//修改规格方法
	function updateSpecification() {
		const optionList = [];
		const trs = $("#updateSpecificationTable tbody tr")
		for(let i=0; i<trs.length; i++){
			const tds = $(trs[i]).find("td");
			const optionvalue = $(tds[0]).find("input").val();
			const order = $(tds[1]).find("input").val();
			const options = {"optionName": optionvalue, "orders": order};
			optionList.push(options);//对象js 对象 java不好接受，把对象转成json字符串，后台java转成list集合
		}
		var specName = $("#updateSpecName").val();
		var id = $("#updateId").val();
		var json = JSON.stringify(optionList);
		//ajax请求修改规格
		$.post("/specification/updateSpecification",{specName:specName,id:id,json:json},function (result) {
			if("ok"==result.message){
				window.location.reload()
			}else {
				alert("修改失败")
			}
		})
	}

	//删除方法
	function deleteBrand() {
		var rows = $("input[name='ids']:checked")
		var ids = [];
		var data = {ids : null}
		$(rows).each(function (i,e) {
			if (this.checked){
				ids.push($(e).val())
			}
		})
		console.log(ids)
		if(ids.length <= 0){
			alert("请选择")
			return;
		}
		data.ids=ids
		console.log(data)
		$.post("/specification/deleteSpecificationBatch",data,function (result) {
			if("ok"==result.message){
				window.location.reload()
			}else {
				alert("修改失败")
			}
		})
		console.log(ids)
	}
</script>
</html>